package com.java.main.time;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

/**
 * @author 顾志杰
 * @date 2021/5/31-8:47
 */
public class Time {


    public static void main(String[] args) throws Exception {
//        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        LocalDate date = LocalDate.parse("2020-03-08");
//        date.atStartOfDay();
//        System.out.println(date.plusDays(100));
//        System.out.println(getStartOrEndDayOfQuarter(true));
//        System.out.println(getFirstYear());
//        System.out.println(new Date().getTime());
//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
//        String[] str = new String[]{"A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "S", "Y", "Z"};
//        Date date = new Date();
//        String one = dateFormat.format(date);
//        Random r = new Random();
//        int a = r.nextInt(24);
//        int b = r.nextInt(24);
//        String s1 = str[a];
//        String s2 = str[b];
//        one += s1;
//        one += s2;
//        System.out.println(one);
//        LocalDateTime now = LocalDateTime.now();
//
//        System.out.println(now.getMonthValue());

//
//        NumberFormat formatter = NumberFormat.getNumberInstance();
//        formatter.setMinimumIntegerDigits(4);
//        formatter.setGroupingUsed(false);
//        String numStr = formatter.format(11);
//        System.out.println(numStr);

//        BigDecimal decimal = new BigDecimal("1.12345");
//        System.out.println(decimal);
//        BigDecimal setScale = decimal.setScale(4,BigDecimal.ROUND_HALF_DOWN);
//        System.out.println(setScale);
//
//        BigDecimal setScale1 = decimal.setScale(4,BigDecimal.ROUND_HALF_UP);
//        System.out.println(setScale1);

//        System.out.println(getRandomString(16).length());;
//        int i = 0/0;


    }

    private static String getRandomString(int length) {
        String str = "000110012023102016977941857827V72BWV1   0001ROBQ174SBJE42N0H23050162755000001536            23050162755000002173            6217000990008858884             01中国建设银行甘南支行                                                            null                                                                                10000.00        其他费用                                                                                                                        2023102020231023";
        return str;
    }
    /**
     * 两个时间差多少天
     * @param dateFormat
     * @throws Exception
     */
    private void a(DateFormat dateFormat)throws Exception {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(dateFormat.parse("2021-01-01 17:00:00"));
        calendar.add(Calendar.DATE,14);
        Date d=calendar.getTime();
        long diff = d.getTime() - new Date().getTime();
        System.out.println(String.format("%.2f",(double) diff / (1000 * 60 * 60 * 24)));


        //localdatetime差多少时间
        LocalDateTime star = LocalDateTime.now();
        LocalDateTime end = star.plusDays(2);
        Duration between = Duration.between(star, end);
        //差多少天
        long l1 = between.toDays();
    }

    /**
     * 时间加上多少天 月 年
     */
    private void b()throws Exception {
        String month ="2019-07-01";
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        Date startTime = f.parse(month);
        Calendar cal = Calendar.getInstance();
        cal.setTime(startTime);
        cal.add(Calendar.MONTH,1);
        System.out.println(f.format(cal.getTime()));
    }

    /**
     * localdate加上多少天 月 年
     */
    private void c(){
        //三天后
        LocalDate afterThreeDays = LocalDate.now().plusDays(3);
        //三天前
        LocalDate threeDaysAgo = LocalDate.now().minusDays(3);

        LocalDate localDate1 = LocalDate.now();
        LocalDate date = LocalDate.parse("2020-01-30");
        long day1 = localDate1.toEpochDay();
        long day2 = date.toEpochDay();
        System.out.println(day1);
        //相差多少天
        long day = day2-day1;
    }


    /**
     * 获取当前日期所在季度的开始日期和结束日期
     * 季度一年四季， 第一季度：1月-3月， 第二季度：4月-6月， 第三季度：7月-9月， 第四季度：10月-12月
     * @param isFirst  true表示查询本季度开始日期  false表示查询本季度结束日期
     * @return
     */
    public static LocalDate getStartOrEndDayOfQuarter(Boolean isFirst){
        LocalDate today=LocalDate.now();
        LocalDate resDate = LocalDate.now();
        if (today == null) {
            today = resDate;
        }
        Month month = today.getMonth();
        Month firstMonthOfQuarter = month.firstMonthOfQuarter();
        Month endMonthOfQuarter = Month.of(firstMonthOfQuarter.getValue() + 2);
        if (isFirst) {
            resDate = LocalDate.of(today.getYear(), firstMonthOfQuarter, 1);
        } else {
            resDate = LocalDate.of(today.getYear(), endMonthOfQuarter, endMonthOfQuarter.length(today.isLeapYear()));
        }
        return resDate;
    }

    /**
     * 获取周一
     * @return
     */
    public static LocalDate getFirstWeek(){
        LocalDate now = LocalDate.now();
        LocalDate with = now.with(DayOfWeek.MONDAY);
        return with;
    }

    /**
     * 获取1号
     * @return
     */
    public static LocalDate getFirstMonth(){
        LocalDate now = LocalDate.now();
        LocalDate with = now.with(TemporalAdjusters.firstDayOfMonth());
        return with;
    }

    /**
     * 获取1月1号
     * @return
     */
    public static LocalDate getFirstYear(){
        LocalDate now = LocalDate.now();
        LocalDate with = now.with(TemporalAdjusters.firstDayOfYear());
        return with;
    }

    private Timer lockTimer = new Timer();

    // 定时器
    private void timer() {
        lockTimer.schedule(new TimerTask() {
                               @Override
                               public void run() {
                                   System.out.println("自动续期，重置到30秒");
                               }
                           },
                3*1000,
                3*1000);
    }


}
